package com.xigege.netty.nettyComponent.handlerAndPipeline;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import lombok.Builder;
import lombok.Data;

import java.net.InetSocketAddress;
import java.nio.charset.Charset;

public class TestPipeline {
    public static void main(String[] args) {
        new ServerBootstrap()
                .group(new NioEventLoopGroup(), new NioEventLoopGroup(2))
                .channel(NioServerSocketChannel.class)
                // 默认添加headHandler, tailHandler
                // 入站处理器 从头往后走，出战处理器从后往前走
                .childHandler(new ChannelInitializer<NioSocketChannel>() {
                    @Override
                    protected void initChannel(NioSocketChannel ch) throws Exception {
                        ch.pipeline().addLast("h1", new ChannelInboundHandlerAdapter() {
                            @Override
                            public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                                System.out.println(1);
                                ByteBuf buf = (ByteBuf) msg;
                                String name = buf.toString(Charset.defaultCharset());
                                super.channelRead(ctx, name);
                            }
                        });
                        ch.pipeline().addLast("h2", new ChannelInboundHandlerAdapter() {
                            @Override
                            public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                                System.out.println(2);
                                Student student = Student.builder().name(msg.toString()).build();
                                // 将数据传递给下个handler，如果不调用，调用链会断开或者调用  ctx.fireChannelRead(msg);
                                super.channelRead(ctx, student);
                            }
                        });

                        ch.pipeline().addLast("h4", new ChannelOutboundHandlerAdapter() {
                            @Override
                            public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
                                System.out.println(4);
                                super.write(ctx, msg, promise);
                            }
                        });

                        ch.pipeline().addLast("h3", new ChannelInboundHandlerAdapter() {
                            @Override
                            public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                                System.out.println(3);
                                System.out.println("结果：" + msg + "，class：" + msg.getClass());
                                // 拿到channel写数据，从tail开始从后往前找OutboundHandler出站处理器，找到后执行
//                                ch.writeAndFlush(ctx.alloc().buffer().writeBytes("server send msg...".getBytes()));
                                // 拿到ChannelHandlerContext写数据，从当前的InboundHandler从后往前找OutboundHandler出站处理器，找到后执行
                                ctx.writeAndFlush(ctx.alloc().buffer().writeBytes("server send msg...".getBytes()));
                            }
                        });

                        ch.pipeline().addLast("h5", new ChannelOutboundHandlerAdapter() {
                            @Override
                            public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
                                System.out.println(5);
                                super.write(ctx, msg, promise);
                            }
                        });

                        ch.pipeline().addLast("h6", new ChannelOutboundHandlerAdapter() {
                            @Override
                            public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
                                System.out.println(6);
                                super.write(ctx, msg, promise);
                            }
                        });

                    }
                }).bind(new InetSocketAddress(6666));
    }

    @Data
    @Builder
    static class Student {
        String name;
    }
}
